package cn.lu.train.mvp.model;

import java.io.File;

import cn.lu.train.api.UserAPI;
import cn.lu.train.entry.ResultData;
import cn.lu.train.entry.User;
import cn.lu.train.net.CallBack;
import cn.lu.train.net.NetUtils;
import cn.lu.train.util.TextUtils;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserModel extends BaseModel<UserModel> {

    public void login(String phone, String password, final Callback<ResultData<User>> callback){
        NetUtils.get()
                .retrofit()
                .create(UserAPI.class)
                .login(phone, password)
                .enqueue(new CallBack<>(callback));
    }

    public void updateUserInfo(File headImg, String nickName, String sex, String birth, String city, String sign, Callback<ResultData<User>> callback){
        MultipartBody.Builder multiBodyBuilder = null;
        FormBody.Builder formBodyBuilder = null;

        boolean hasFile = false;
        if (headImg != null && headImg.isFile()) {
            multiBodyBuilder = new MultipartBody.Builder();
            multiBodyBuilder.setType(MultipartBody.FORM);
            multiBodyBuilder.addFormDataPart("headimg", headImg.getName(), RequestBody.create(MediaType.parse("image/*"), headImg));
            hasFile = true;
        }
        if (multiBodyBuilder == null) {
            formBodyBuilder = new FormBody.Builder();
        }

        if (!TextUtils.isEmpty(nickName)) {
            addData(multiBodyBuilder, formBodyBuilder, "name", nickName);
        }
        if (!TextUtils.isEmpty(sex)) {
            addData(multiBodyBuilder, formBodyBuilder, "sex", sex);
        }
        if (!TextUtils.isEmpty(birth)) {
            addData(multiBodyBuilder, formBodyBuilder, "birth", birth);
        }
        if (!TextUtils.isEmpty(city)) {
            addData(multiBodyBuilder, formBodyBuilder, "city", city);
        }
        if (!TextUtils.isEmpty(sign)) {
            addData(multiBodyBuilder, formBodyBuilder, "sign", sign);
        }

        RequestBody body = null;
        if (hasFile) {
            body = multiBodyBuilder.build();
        } else {
            FormBody formBody = formBodyBuilder.build();
            if (formBody.size() < 1) {
                return;
            }
            body = formBody;
        }

        NetUtils.get()
                .retrofit()
                .create(UserAPI.class)
                .updateUserInfo(body, hasFile)
                .enqueue(new CallBack<>(callback));
    }

    private void addData(MultipartBody.Builder mbuilder, FormBody.Builder fbuilder, String name, String value) {
        if (mbuilder != null) {
            mbuilder.addFormDataPart(name, value);
        } else {
            fbuilder.add(name, value);
        }
    }

    public void getUserInfo(final Callback<ResultData<User>> callback){
        NetUtils.get()
                .retrofit()
                .create(UserAPI.class)
                .getUserInfo()
                .enqueue(new CallBack<>(callback));
    }

    public void getMineInfo(final Callback<ResultData<User>> callback){
        NetUtils.get()
                .retrofit()
                .create(UserAPI.class)
                .getMineInfo()
                .enqueue(new CallBack<>(callback));
    }
}
